package com.ohuang.util_h;

import android.os.SystemClock;
import android.view.MotionEvent;
import android.view.View;

public class TouchEventUtil {

    /**
     * 执行点击坐标(使用屏幕坐标)
     * @param view
     * @param x
     * @param y
     */
    public static void performTouchEventClickForScreenXY(View view, int x, int y) {
        int[] location = new int[2];
        view.getLocationInWindow(location);
        performTouchEventClick(view, x - location[0], y - location[1]);
    }
    /**
     * 执行点击坐标(view内部坐标)
     * @param view
     * @param x
     * @param y
     */
    public static void performTouchEventClick(View view, float x, float y) {
        long downTime = SystemClock.uptimeMillis();
        MotionEvent downEvent = MotionEvent.obtain(
                downTime, downTime,
                MotionEvent.ACTION_DOWN,
                x, y, 0
        );
        downTime += 1000;
        MotionEvent upEvent = MotionEvent.obtain(
                downTime, downTime,
                MotionEvent.ACTION_UP,
                x, y, 0
        );
        view.dispatchTouchEvent(downEvent);
        view.dispatchTouchEvent(upEvent);
        downEvent.recycle();
        upEvent.recycle();
    }
}
